package com.chh.client;

import com.baidu.bjf.remoting.protobuf.Codec;
import com.baidu.bjf.remoting.protobuf.ProtobufProxy;
import com.chh.jproto.HouseJProto;
import com.chh.jproto.PeopleJProto;
import com.chh.util.FileUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

/**
 * @author 一碗情深
 * @since 2024-08-09 星期五 16:33:08
 */
public class ClientRequest {
    public static void main(String[] args) throws IOException {
        byte[] data = FileUtils.readFileByte("JProtobuf");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_PROTOBUF);

        HttpEntity<byte[]> request = new HttpEntity<>(data, headers);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<byte[]> response = restTemplate.postForEntity("http://localhost:8088/api/house/index", request, byte[].class);

        System.out.println(response);

        Codec<HouseJProto> houseJProtoCodec = ProtobufProxy.create(HouseJProto.class);
        HouseJProto houseJProto = houseJProtoCodec.decode(response.getBody());
        System.out.println(houseJProto);

        PeopleJProto peopleJProto = houseJProto.getOwnerExtent().unpack(PeopleJProto.class);
        System.out.println(peopleJProto);
    }
}
